package com.changpay.zywy.ui.fragment.service

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ApplyDetailAdapter
import com.changpay.zywy.adapter.ChangeCardInfoAdapter
import com.changpay.zywy.adapter.SelfMachineAdapter
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.bean.*
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.mvp.contract.ApplyDetailContract
import com.changpay.zywy.mvp.presenter.ApplyDetailPresenter
import com.changpay.zywy.ui.activity.serivce.ApplyDetailAllowActivity
import com.changpay.zywy.ui.activity.serivce.JieSuanKaDetailActivity
import com.changpay.zywy.ui.activity.serivce.ZhiBeiJiDetailActivity
import com.changpay.zywy.wight.CustomLoadMoreView
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class ApplyDetailFragment : BaseMvpFragment<ApplyDetailContract.View, ApplyDetailContract.Presenter>(),
    ApplyDetailContract.View {
    override fun showAllocationInfo(data: ApplyDetailBean) {
        data.let {
            when (position) {
                0 -> {
                    mAdapter_one.run {
                        it.data?.let {
                            addData(it)
                            val size = it.size
                            if (size < 10) {
                                loadMoreEnd(true)
                            } else {
                                loadMoreComplete()
                            }
                        }
                    }
                }
                1 -> {
                    mAdapter_two.run {
                        it.data?.let {
                            addData(it)
                            val size = it.size
                            if (size < 10) {
                                loadMoreEnd(true)
                            } else {
                                loadMoreComplete()
                            }
                        }
                    }
                }
                2 -> {
                    mAdapter_three.run {
                        it.data?.let {
                            addData(it)
                            val size = it.size
                            if (size < 10) {
                                loadMoreEnd(true)
                            } else {
                                loadMoreComplete()
                            }
                        }
                    }
                }
                else -> showToast("未知错误")
            }
        }
    }

    override fun showSelfMachineInfo(data: SelfMachineDetailBean) {
        data.let {
            mAdapter_four.run {
                it.data?.let {
                    addData(it)
                    val size = it.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
            }
        }

    }

    override fun showCardChangeInfo(data: CardChangeDetailBean) {
        data.let {
            mAdapter_five.run {
                it.data?.let {
                    addData(it)
                    val size = it.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
            }
        }

    }

    private var page = 1
    private val pagesize = "10"


    private var position: Int = 0  // 当前viewPage的位置


    companion object {
        fun getInstance(instanceType: Int): ApplyDetailFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            val fragment = ApplyDetailFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun createPresenter(): ApplyDetailContract.Presenter = ApplyDetailPresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()
    }

    fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["page"] = page.toString()
        param["pagesize"] = pagesize
        when (position) {
            0 -> {
                param["type"] = "0"
                mPresenter?.getAllocationInfo(param)
            }
            1 -> {
                param["type"] = "2"
                mPresenter?.getAllocationInfo(param)
            }
            2 -> {
                param["type"] = "1"
                mPresenter?.getAllocationInfo(param)
            }
            3 -> {
                param["type"] = "3"
                mPresenter?.getSelfMachineInfo(param)
            }
            4 -> {
                param["type"] = "4"
                mPresenter?.getCardChangeInfo(param)
            }
        }
    }

    override fun initView(view: View) {
        super.initView(view)
        position = arguments?.getInt("position") ?: 0
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas_one = mutableListOf<ApplyDetailBean.DataBean>()
    private val datas_two = mutableListOf<ApplyDetailBean.DataBean>()
    private val datas_three = mutableListOf<ApplyDetailBean.DataBean>()
    private val datas_four = mutableListOf<SelfMachineDetailBean.DataBean>()
    private val datas_five = mutableListOf<CardChangeDetailBean.DataBean>()

    /**
     * Adapter
     */
    private val mAdapter_one: ApplyDetailAdapter by lazy {
        ApplyDetailAdapter(context, datas_one)
    }
    private val mAdapter_two: ApplyDetailAdapter by lazy {
        ApplyDetailAdapter(context, datas_two)
    }
    private val mAdapter_three: ApplyDetailAdapter by lazy {
        ApplyDetailAdapter(context, datas_three)
    }
    private val mAdapter_four: SelfMachineAdapter by lazy {
        SelfMachineAdapter(context, datas_four)
    }
    private val mAdapter_five: ChangeCardInfoAdapter by lazy {
        ChangeCardInfoAdapter(context, datas_five)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {

        when (position) {
            0 -> page = mAdapter_one.data.size / 10 + 1
            1 -> page = mAdapter_two.data.size / 10 + 1
            2 -> page = mAdapter_three.data.size / 10 + 1
            3 -> page = mAdapter_four.data.size / 10 + 1
            4 -> page = mAdapter_five.data.size / 10 + 1
        }
        loadData()

    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        when (this.position) {
            0 -> {
                if (datas_one.size != 0) {
                    val data = datas_one[position]
                    Intent(activity, ApplyDetailAllowActivity::class.java).run {
                        putExtra("id", data.id.toString())
                        startActivity(this)
                    }
                }
            }
            1 -> {
                if (datas_two.size != 0) {
                    val data = datas_two[position]
                    Intent(activity, ApplyDetailAllowActivity::class.java).run {
                        putExtra("id", data.id.toString())
                        startActivity(this)
                    }
                }
            }
            2 -> {
                if (datas_three.size != 0) {
                    val data = datas_three[position]
                    Intent(activity, ApplyDetailAllowActivity::class.java).run {
                        putExtra("id", data.id.toString())
                        startActivity(this)
                    }
                }

            }
            3 -> {
                if (datas_four.size != 0) {
                    val data = datas_four[position]
                    val intent = Intent(activity, ZhiBeiJiDetailActivity::class.java)
                    intent.putExtra("manufacturer_name", data.manufacturer_name)
                    intent.putExtra("machine_model", data.machine_model)
                    intent.putExtra("data_content", data.data_content)
                    startActivity(intent)
                }

            }
            4 -> {
                if (datas_five.size != 0) {
                    val data = datas_five[position]
                    val intent = Intent(activity, JieSuanKaDetailActivity::class.java)
                    intent.putExtra("merchant_id", data.merchant_id)
                    intent.putExtra("card_id", data.card_id)
                    intent.putExtra("deposit_bank", data.deposit_bank)
                    intent.putExtra("bank_branch", data.bank_branch)
                    intent.putExtra("phone_num", data.phone_num)
                    intent.putExtra("created_time", data.created_time)
                    intent.putExtra("audit_describe", data.audit_describe)
                    intent.putExtra("balance_examine_type", data.balance_examine_type)
                    startActivity(intent)
                }

            }

        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->

    }


    private fun initRecyclerView() {

        recyclerView.run {
            layoutManager = linearLayoutManager
            when (position) {
                0 -> adapter = mAdapter_one
                1 -> adapter = mAdapter_two
                2 -> adapter = mAdapter_three
                2 -> adapter = mAdapter_four
                2 -> adapter = mAdapter_five
            }
            itemAnimator = DefaultItemAnimator()
        }
        when (position) {
            0 -> {
                mAdapter_one.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@ApplyDetailFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }
            }
            1 -> {
                mAdapter_two.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@ApplyDetailFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }
            }
            2 -> {
                mAdapter_three.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@ApplyDetailFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }

            }
            3 -> {
                mAdapter_four.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@ApplyDetailFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }

            }
            4 -> {
                mAdapter_five.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@ApplyDetailFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }

            }
        }
    }
}






